package com.novoda.runbuddy.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Spinner;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.IMapOfJogsController;
import com.novoda.runbuddy.controller.MapOfJogsControllerImpl;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;

public class MapOfJogs extends MapActivity {

	private IMapOfJogsController	mapOfJogController;
	public LocationManager			locationManager		= null;
	protected LocationProvider		provider;
	protected MyIntentReceiver		myIntentReceiver	= null;
	private static final String		PROVIDERNAME		= "testjog";
	private static final String		TAG					= "MAPOFJOG";
	final Handler					mHandler			= new Handler();
	final int						MAPOFJOG			= 3333;
	Bundle							extras;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		int day = 0;
		int month = 0;
		int year = 0;

		this.setContentView(R.layout.map_of_jogs_view);

		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		mapOfJogController = new MapOfJogsControllerImpl(this);
		extras = getIntent().getExtras();

		if (extras == null) {
			Calendar cal = new GregorianCalendar();
			day = cal.get(Calendar.DAY_OF_WEEK);
			month = cal.get(Calendar.MONTH);
			year = cal.get(Calendar.YEAR);
		} else {
			day = extras.getInt("day");
			month = extras.getInt("month");
			year = extras.getInt("year");
		}

		mapOfJogController.createMapOfJogView((MapView) findViewById(R.id.map), (Spinner) findViewById(R.id.spinner1),
				android.R.layout.simple_spinner_dropdown_item, (Button) findViewById(R.id.go), day, month, year);

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
			case KeyEvent.KEYCODE_BACK:
				finish();
				return true;
		}
		return mapOfJogController.onKeyDown(keyCode, event);
	}

	class MyIntentReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.v(TAG, "Updating a jogging a current jogging route.");
			
			Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			Log.i(TAG, "Updating longitude: " + location.getLongitude() + ", latitude: " + location.getLatitude());
			mapOfJogController.setCurrentGPSLocationOfPhone(location);
			mapOfJogController.updateRunningPath();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}